
#define LV_CONF_INCLUDE_SIMPLE 1

#include "lv_conf.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h> // 包含线程头文件
#include "ui.h"
#include "lvgl/src/core/lv_obj.h" // 包含LVGL对象头文件
#include "lvgl/lvgl.h"
#include "menu.h"
#include "ui_custom.h"

// 命令枚举
typedef enum {
    CMD_UNKNOWN,  // 未知命令
    CMD_LEFT_ON,  // 左转向灯开
    CMD_LEFT_OFF, // 左转向灯关
    CMD_RIGHT_ON, // 右转向灯开
    CMD_RIGHT_OFF, // 右转向灯关
    CMD_DOUBLE_LED,    // 双闪灯
    CMD_SPEED_SET,     // 速度设置命令
    CMD_RPM_SET,       // 转速度设置命令
    CMD_FUEL_SET,      // 油量设置命令
    CMD_COOLANT_TEMP,  // 冷却液温度命令
    CMD_ABS_WARNING,   // ABS故障警告
    CMD_ABS_NORMAL,    // ABS故障恢复
    CMD_BRAKE_WARNING, // 刹车故障警告
    CMD_BRAKE_NORMAL,  // 刹车故障恢复
    CMD_FUEL_WARNING,  // 燃油警告
    CMD_FUEL_NORMAL,   // 燃油正常
    CMD_DOOR_WARNING,  // 车门未关警告
    CMD_DOOR_NORMAL,   // 车门正常
    CMD_AIRBAG_WARNING,   // 安全气囊故障警告
    CMD_AIRBAG_NORMAL,    // 安全气囊故障恢复
    CMD_SEATBELT_WARNING, // 安全带未系警告
    CMD_SEATBELT_NORMAL,  // 安全带正常
    CMD_HIGH_BEAM_ON,     // 远光灯开
    CMD_HIGH_BEAM_OFF,    // 远光灯关
    CMD_LOW_BEAM_ON,      // 近光灯开
    CMD_LOW_BEAM_OFF,     // 近光灯关
    CMD_HAZARD_ON,        // 双闪灯开
    CMD_HAZARD_OFF,       // 双闪灯关
    CMD_EXIT
} CommandType;

// 在全局变量区域添加定时器变量
static lv_timer_t *left_turn_timer = NULL;
static lv_timer_t *right_turn_timer = NULL;
static lv_timer_t *double_flash_timer = NULL;

// 在全局变量区域添加双闪灯状态变量
static int hazard_warning_state = 0;
static lv_timer_t *hazard_warning_timer = NULL;

// 添加双闪灯定时器回调函数
static void hazard_warning_timer_cb(lv_timer_t *timer) {
    static int state = 0;
    if (state) {
        lv_obj_add_flag(ui_Left_turn_signal, LV_OBJ_FLAG_HIDDEN);
        lv_obj_add_flag(ui_Right_turn_signal, LV_OBJ_FLAG_HIDDEN);
    } else {
        lv_obj_clear_flag(ui_Left_turn_signal, LV_OBJ_FLAG_HIDDEN);
        lv_obj_clear_flag(ui_Right_turn_signal, LV_OBJ_FLAG_HIDDEN);
    }
    state = !state;
}


// 全局变量用于存储线程ID
pthread_t thread_id;

// 转向灯状态
static int left_turn_signal_state = 0;
static int right_turn_signal_state = 0;
static int double_flash_state = 0;

// 冷却液温度相关变量
static float coolant_temperature = 40.0f;
extern bool is_manual_temp_set;  // 使用外部变量
static const float COOLANT_TEMP_WARNING_THRESHOLD = 120.0f;

// 左转向灯定时器回调函数
static void left_turn_timer_cb(lv_timer_t *timer) {
    static int state = 0;
    if (state) {
        lv_obj_add_flag(ui_Left_turn_signal, LV_OBJ_FLAG_HIDDEN);
    } else {
        lv_obj_clear_flag(ui_Left_turn_signal, LV_OBJ_FLAG_HIDDEN);
    }
    state = !state;
}

// 右转向灯定时器回调函数
static void right_turn_timer_cb(lv_timer_t *timer) {
    static int state = 0;
    if (state) {
        lv_obj_add_flag(ui_Right_turn_signal, LV_OBJ_FLAG_HIDDEN);
    } else {
        lv_obj_clear_flag(ui_Right_turn_signal, LV_OBJ_FLAG_HIDDEN);
    }
    state = !state;
}

// 双闪定时器回调函数
static void double_flash_timer_cb(lv_timer_t *timer) {
    static int state = 0;
    if (state) {
        lv_obj_add_flag(ui_Left_turn_signal, LV_OBJ_FLAG_HIDDEN);
        lv_obj_add_flag(ui_Right_turn_signal, LV_OBJ_FLAG_HIDDEN);
    } else {
        lv_obj_clear_flag(ui_Left_turn_signal, LV_OBJ_FLAG_HIDDEN);
        lv_obj_clear_flag(ui_Right_turn_signal, LV_OBJ_FLAG_HIDDEN);
    }
    state = !state;
}

// 命令解析函数
CommandType parse_command(const char* input) {
    if(strcmp(input, "left_on") == 0) return CMD_LEFT_ON;
    if(strcmp(input, "left_off") == 0) return CMD_LEFT_OFF;
    if(strcmp(input, "right_on") == 0) return CMD_RIGHT_ON;
    if(strcmp(input, "right_off") == 0) return CMD_RIGHT_OFF;
    if(strcmp(input, "double_led") == 0) return CMD_DOUBLE_LED;
    if(strncmp(input, "speed ", 6) == 0) return CMD_SPEED_SET;   // 速度设置命令格式: "speed XX.X"
    if(strncmp(input, "rpm ", 4) == 0) return CMD_RPM_SET;       // 转速设置命令格式: "rpm X.X"
    if(strncmp(input, "coolant_temp ", 13) == 0) return CMD_COOLANT_TEMP;// 冷却液温度命令格式: "coolant_temp XX.X"
    if(strncmp(input, "fuel ", 5) == 0) return CMD_FUEL_SET;      // 油量设置命令格式: "fuel X/X"
     
    // 其他报警灯命令
    if(strcmp(input, "abs_warning") == 0) return CMD_ABS_WARNING;
    if(strcmp(input, "abs_normal") == 0) return CMD_ABS_NORMAL;
    if(strcmp(input, "brake_warning") == 0) return CMD_BRAKE_WARNING;
    if(strcmp(input, "brake_normal") == 0) return CMD_BRAKE_NORMAL;
    if(strcmp(input, "fuel_warning") == 0) return CMD_FUEL_WARNING;
    if(strcmp(input, "fuel_normal") == 0) return CMD_FUEL_NORMAL;
    if(strcmp(input, "door_warning") == 0) return CMD_DOOR_WARNING;
    if(strcmp(input, "door_normal") == 0) return CMD_DOOR_NORMAL;
    if(strcmp(input, "airbag_warning") == 0) return CMD_AIRBAG_WARNING;
    if(strcmp(input, "airbag_normal") == 0) return CMD_AIRBAG_NORMAL;
    if(strcmp(input, "seatbelt_warning") == 0) return CMD_SEATBELT_WARNING;
    if(strcmp(input, "seatbelt_normal") == 0) return CMD_SEATBELT_NORMAL;
    if(strcmp(input, "high_beam_on") == 0) return CMD_HIGH_BEAM_ON;
    if(strcmp(input, "high_beam_off") == 0) return CMD_HIGH_BEAM_OFF;
    if(strcmp(input, "low_beam_on") == 0) return CMD_LOW_BEAM_ON;
    if(strcmp(input, "low_beam_off") == 0) return CMD_LOW_BEAM_OFF;
    if(strcmp(input, "hazard_on") == 0) return CMD_HAZARD_ON;
    if(strcmp(input, "hazard_off") == 0) return CMD_HAZARD_OFF;
    if(strcmp(input, "exit") == 0) return CMD_EXIT;

    return CMD_UNKNOWN;
}

// 终端输入线程函数
void* terminal_input(void* arg) {
    char input[50];  // 增加缓冲区大小以支持更复杂的命令
    
    // 初始化所有警告灯为半透明状态
    init_warning_lights();
    
    // 初始化转向灯状态
    lv_obj_add_flag(ui_Left_turn_signal, LV_OBJ_FLAG_HIDDEN);
    lv_obj_add_flag(ui_Right_turn_signal, LV_OBJ_FLAG_HIDDEN);

    while(1) {
        printf("Enter command (left_on, left_off, right_on, right_off, double_led, speed [0-220], rpm [0.0-7.0], fuel [0/1-1/1], coolant_temp [40.0-120.0], abs_warning, abs_normal, brake_warning, brake_normal, fuel_warning, fuel_normal, door_warning, door_normal, airbag_warning, airbag_normal, seatbelt_warning, seatbelt_normal, high_beam_on, high_beam_off, low_beam_on, low_beam_off, hazard_on, hazard_off, exit): ");
        fgets(input, sizeof(input), stdin);
        
        // 去除换行符
        input[strcspn(input, "\n")] = 0;
        
        // 解析命令
        CommandType cmd = parse_command(input);
        
        // 使用switch语句处理命令
        switch(cmd) {
            case CMD_LEFT_ON:
                // 如果双闪正在运行，先停止双闪
                if (double_flash_state) {
                    double_flash_state = 0;
                    if (double_flash_timer != NULL) {
                        lv_timer_del(double_flash_timer);
                        double_flash_timer = NULL;
                    }
                }
                // 如果右转向灯正在运行，先停止右转向灯,并隐藏它
                if (right_turn_signal_state) {
                    right_turn_signal_state = 0;
                    if (right_turn_timer != NULL) {
                        lv_timer_del(right_turn_timer);
                        right_turn_timer = NULL;
                    }
                    lv_obj_add_flag(ui_Right_turn_signal, LV_OBJ_FLAG_HIDDEN);
                }
                
                left_turn_signal_state = 1;
                // 创建左转向灯定时器，每500ms切换一次状态
                if (left_turn_timer == NULL) {
                    left_turn_timer = lv_timer_create(left_turn_timer_cb, 500, NULL);
                }
                break;
                
            case CMD_LEFT_OFF:
                left_turn_signal_state = 0;
                // 删除左转向灯定时器并隐藏转向灯
                if (left_turn_timer != NULL) {
                    lv_timer_del(left_turn_timer);
                    left_turn_timer = NULL;
                }
                lv_obj_add_flag(ui_Left_turn_signal, LV_OBJ_FLAG_HIDDEN);
                break;
                
            case CMD_RIGHT_ON:
                // 如果双闪正在运行，先停止双闪
                if (double_flash_state) {
                    double_flash_state = 0;
                    if (double_flash_timer != NULL) {
                        lv_timer_del(double_flash_timer);
                        double_flash_timer = NULL;
                    }
                }
                // 如果左转向灯正在运行，先停止左转向灯,并隐藏它
                if (left_turn_signal_state) {
                    left_turn_signal_state = 0;
                    if (left_turn_timer != NULL) {
                        lv_timer_del(left_turn_timer);
                        left_turn_timer = NULL;
                    }
                    lv_obj_add_flag(ui_Left_turn_signal, LV_OBJ_FLAG_HIDDEN);
                }
                
                right_turn_signal_state = 1;
                // 创建右转向灯定时器，每500ms切换一次状态
                if (right_turn_timer == NULL) {
                    right_turn_timer = lv_timer_create(right_turn_timer_cb, 500, NULL);
                }
                break;
                
            case CMD_RIGHT_OFF:
                right_turn_signal_state = 0;
                // 删除右转向灯定时器并隐藏转向灯
                if (right_turn_timer != NULL) {
                    lv_timer_del(right_turn_timer);
                    right_turn_timer = NULL;
                }
                lv_obj_add_flag(ui_Right_turn_signal, LV_OBJ_FLAG_HIDDEN);
                break;
                
            case CMD_DOUBLE_LED:
                // 切换双闪状态
                double_flash_state = !double_flash_state;
                
                if (double_flash_state) {
                    // 如果左转向灯或右转向灯正在运行，先停止它们
                    if (left_turn_signal_state) {
                        left_turn_signal_state = 0;
                        if (left_turn_timer != NULL) {
                            lv_timer_del(left_turn_timer);
                            left_turn_timer = NULL;
                        }
                    }
                    
                    if (right_turn_signal_state) {
                        right_turn_signal_state = 0;
                        if (right_turn_timer != NULL) {
                            lv_timer_del(right_turn_timer);
                            right_turn_timer = NULL;
                        }
                    }
                    
                    // 创建双闪定时器，每500ms切换一次状态
                    if (double_flash_timer == NULL) {
                        double_flash_timer = lv_timer_create(double_flash_timer_cb, 500, NULL);
                    }
                } else {
                    // 停止双闪
                    if (double_flash_timer != NULL) {
                        lv_timer_del(double_flash_timer);
                        double_flash_timer = NULL;
                    }
                    lv_obj_add_flag(ui_Left_turn_signal, LV_OBJ_FLAG_HIDDEN);
                    lv_obj_add_flag(ui_Right_turn_signal, LV_OBJ_FLAG_HIDDEN);
                }
                break;
                
            case CMD_COOLANT_TEMP:
                // 解析温度值
                {
                    float temp;
                    if (sscanf(input, "coolant_temp %f", &temp) == 1)
                    {
                        if (temp >= 40.0f && temp <= 120.0f)
                        {
                            coolant_temperature = temp;
                            is_manual_temp_set = true;  // 设置手动设置温度标志
                            printf("Coolant temperature set to: %.1f°C\n", coolant_temperature);
                            // 直接调用设置温度的函数
                            set_coolant_temp(temp);
                        }
                        else
                        {
                            printf("Temperature value must be between 40 and 120°C\n");
                        }
                    }
                    else
                    {
                        printf("Invalid temperature format. Use: coolant_temp [value]\n");
                    }
                }
                break;

            case CMD_ABS_WARNING:
                set_abs_warning(true);
                break;

            case CMD_ABS_NORMAL:
                set_abs_warning(false);
                break;
                
            case CMD_BRAKE_WARNING:
                set_brake_warning(true);
                break;
                
            case CMD_BRAKE_NORMAL:
                set_brake_warning(false);
                break;
                
            case CMD_FUEL_WARNING:
                set_fuel_warning(true);
                break;
                
            case CMD_FUEL_NORMAL:
                set_fuel_warning(false);
                break;
                
            case CMD_DOOR_WARNING:
                set_door_warning(true);
                break;
                
            case CMD_DOOR_NORMAL:
                set_door_warning(false);
                break;
                
            case CMD_AIRBAG_WARNING:
                set_airbag_warning(true);
                break;
                
            case CMD_AIRBAG_NORMAL:
                set_airbag_warning(false);
                break;
                
            case CMD_SEATBELT_WARNING:
                set_seatbelt_warning(true);
                break;
                
            case CMD_SEATBELT_NORMAL:
                set_seatbelt_warning(false);
                break;
                
            case CMD_HIGH_BEAM_ON:
                set_high_beam(true);
                break;
                
            case CMD_HIGH_BEAM_OFF:
                set_high_beam(false);
                break;
                
            case CMD_LOW_BEAM_ON:
                set_low_beam(true);
                break;
                
            case CMD_LOW_BEAM_OFF:
                set_low_beam(false);
                break;

            case CMD_HAZARD_ON:
                // 如果左转向灯或右转向灯正在运行，先停止它们
                if (left_turn_signal_state)
                {
                    left_turn_signal_state = 0;
                    if (left_turn_timer != NULL)
                    {
                        lv_timer_del(left_turn_timer);
                        left_turn_timer = NULL;
                    }
                }

                if (right_turn_signal_state)
                {
                    right_turn_signal_state = 0;
                    if (right_turn_timer != NULL)
                    {
                        lv_timer_del(right_turn_timer);
                        right_turn_timer = NULL;
                    }
                }

                // 如果双闪灯正在运行，先停止它们

                if (double_flash_state)
                {
                    double_flash_state = 0;
                    if (double_flash_timer != NULL)
                    {
                        lv_timer_del(double_flash_timer);
                        double_flash_timer = NULL;
                    }
                }
                hazard_warning_state = 1;

                // 创建双闪灯定时器，每500ms切换一次状态
                if (hazard_warning_timer == NULL)
                {
                    hazard_warning_timer = lv_timer_create(hazard_warning_timer_cb, 500, NULL);
                }

                set_hazard_warning(true);   // 开启双闪灯
                break;

            case CMD_HAZARD_OFF:
                hazard_warning_state = 0;
                // 删除双闪灯定时器并隐藏转向灯
                if (hazard_warning_timer != NULL)
                {
                    lv_timer_del(hazard_warning_timer);
                    hazard_warning_timer = NULL;
                }
                lv_obj_add_flag(ui_Left_turn_signal, LV_OBJ_FLAG_HIDDEN);
                lv_obj_add_flag(ui_Right_turn_signal, LV_OBJ_FLAG_HIDDEN);
                set_hazard_warning(false);
                break;

                set_hazard_warning(false);   // 关闭双闪灯
                break;

            case CMD_SPEED_SET:
            {
                int speed;
                if (sscanf(input, "speed %d", &speed) == 1)
                {
                    if (speed >= 0 && speed <= 220)
                    {
                        is_manual_temp_set = false;  // 取消手动设置温度标志
                        is_manual_rpm_set = false;   // 取消手动设置转速标志
                        is_manual_fuel_set = false;  // 取消手动设置油量标志
                        
                        int angle = (speed * 238) / 220;  // 计算对应的角度值 (238度对应220的速度值)
                        // 调用设置速度的函数
                        set_speed_value(speed);
                    }
                    else
                    {
                        printf("Speed value must be between 0 and 220\n");
                    }
                }
                else
                {
                    printf("Invalid speed format. Use: speed [value]\n");
                }
            }
            break;

            case CMD_RPM_SET:    // 设置转速命令
            {
                float rpm;
                if (sscanf(input, "rpm %f", &rpm) == 1)
                {
                    if (rpm >= 1.0f && rpm <= 7.0f)
                    {
                        is_manual_rpm_set = true; // 设置手动设置转速标志
                        printf("RPM set to: %.1f x1000/min\n", rpm);
                        set_rpm_value(rpm);
                    }
                    else
                    {
                        printf("RPM value must be between 1.0 and 7.0\n");
                    }
                }
                else
                {
                    printf("Invalid RPM format. Use: rpm [value]\n");
                }
            }
            break;

            case CMD_FUEL_SET:
            {
                float numerator, denominator;
                if (sscanf(input, "fuel %f/%f", &numerator, &denominator) == 2)
                {
                    float fuel = numerator / denominator;
                    if (fuel >= 0.0f && fuel <= 1.0f)
                    {
                        is_manual_fuel_set = true; // 设置手动设置油量标志
                        printf("Fuel set to: %.0f%%\n", fuel * 100);
                        set_fuel_value(fuel);
                    }
                    else
                    {
                        printf("Fuel value must be between 0 and 1\n");
                    }
                }
                else
                {
                    printf("Invalid fuel format. Use: fuel [numerator]/[denominator]\n");
                }
            }
            break;

            case CMD_EXIT:
                return NULL;

            case CMD_UNKNOWN:
            default:
                printf("Unknown command. Please try again.\n");
                break;
            }
    }

    return NULL;
}

// 初始化菜单系统
void menu_init(void)
{
    // 创建终端输入线程
    if (pthread_create(&thread_id, NULL, terminal_input, NULL) != 0)
    {
        fprintf(stderr, "Failed to create terminal input thread\n");
        return;
    }

    // 设置线程为分离状态，这样线程结束时资源会自动回收
    pthread_detach(thread_id);
    
    printf("Menu system initialized. Enter commands to control turn signals and warning lights.\n");
}

// 清理菜单系统
void menu_cleanup(void) {
    // 等待线程结束
    pthread_join(thread_id, NULL);
    
    // 清理定时器
    if (left_turn_timer != NULL) {
        lv_timer_del(left_turn_timer);
        left_turn_timer = NULL;
    }
    if (right_turn_timer != NULL) {
        lv_timer_del(right_turn_timer);
        right_turn_timer = NULL;
    }
    if (double_flash_timer != NULL) {
        lv_timer_del(double_flash_timer);
        double_flash_timer = NULL;
    }

    if (hazard_warning_timer != NULL)
    {
        lv_timer_del(hazard_warning_timer);
        hazard_warning_timer = NULL;
    }

    // 重置所有警告灯状态
    reset_all_warnings();

    printf("Menu system cleaned up.\n");
}


